//
//  QRScanViewController.swift
//  NativeQR
//
//  Created by Harvey on 2017/10/24.
//  Copyright © 2017年 Harvey. All rights reserved.
//

import Foundation
import UIKit

class QRScanViewController: BaseViewController {
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        title = "扫一扫"
        
        QRScanner.shared
        .scan { (previewLayer) in
            
            let width = UIScreen.main.bounds.size.width - 100
            previewLayer.frame = CGRect(x: 50, y: 100, width: width, height: width)
            self.view.layer.insertSublayer(previewLayer, at: 0)
        }
        .completed { (qrValue) in
            
             print(qrValue)
            
            self.sendUserLogin(barCode: qrValue)
            
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        QRScanner.shared.stopRunning()
    }
    
    public func sendUserLogin( barCode:String){
        
        self.hudLoad()
        
        let dict = ["id": String(UserInfo.shareInstance.userModel().id)] as [String: Any]//UserInfo.shareInstance.userModel().toJSON()
        self.hudLoad()
        
        TempNetTool.post( kHost + kQRSetUser + "?loginId=\(barCode)", parameters: dict, progress: nil, success: { [weak self] (result) in
            self?.hudHide()
            self?.hudLoadMessage("登陆成功")
            if (result != nil) {
                self?.navigationController?.popToRootViewController(animated: true)
            }
            
        }) {[weak self]   (error) in
            self?.hudHide()
            self?.navigationController?.popViewController(animated: true)
        }
    }
}
